<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计数器示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            color: #333;
        }

        .container {
            background-color: #ffffff;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            padding: 40px;
            text-align: center;
            width: 400px;
        }

        h1 {
            font-size: 24px;
            margin-bottom: 20px;
            color: #4CAF50;
        }

        input[type="text"] {
            width: 80%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
        }

        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            margin-top: 10px;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            font-size: 20px;
            margin-top: 20px;
            color: #333;
            font-weight: bold;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>计数器示例</h1>
        <p>请输入一个数字列表，和要计数的值：</p>

        <input type="text" id="numberList" placeholder="请输入数字列表，使用逗号分隔 (例如: 1,2,3,1,1)" />
        <input type="text" id="searchValue" placeholder="请输入要计数的值" />

        <button onclick="countOccurrencesInList()">计数</button>

        <div class="result" id="result"></div>
    </div>

    <script>
        const countOccurrences = (arr, val) =>
            arr.reduce((r, v) => (v === val ? r + 1 : r), 0);

        function countOccurrencesInList() {
            const numberList = document.getElementById('numberList').value;
            const searchValue = document.getElementById('searchValue').value;

            if (!numberList || !searchValue) {
                document.getElementById('result').innerText = "请确保两个输入框都已填写！";
                return;
            }

            const arr = numberList.split(',').map(num => num.trim());
            const val = searchValue.trim();

            const count = countOccurrences(arr, val);

            document.getElementById('result').innerText = `"${val}" 在列表中出现了 ${count} 次`;
        }
    </script>

</body>

</html>